package com.activity.fragment;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.Toast;

import com.example.login.R;

public class BottomMenuActivity extends AppCompatActivity {
    private RadioButton home, shop, center;
    private HomeFragment homeFragment;
    private ShopFragment shopFragment;
    private CenterFragment centerFragment;
    private static boolean isExit = false;

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            isExit = false;
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottom_menu);
        initHome();

        home = findViewById(R.id.menu_home);
        shop = findViewById(R.id.menu_shop);
        center = findViewById(R.id.menu_center);

        home.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    initHome();
                }
            }
        });

        shop.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    initShop();
                }
            }
        });

        center.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    initCenter();
                }
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
            // 利用handler延迟发送更改状态信息
            handler.sendEmptyMessageDelayed(0, 2000);
        } else {
            finish();
            System.exit(0);
        }
    }

    private void initHome(){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        if(homeFragment == null){
            homeFragment = new HomeFragment();
            transaction.add(R.id.menu_fragment, homeFragment);
        }
        hideFragment(transaction);
        transaction.show(homeFragment);
        transaction.commit();
    }

    private void initShop(){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        if(shopFragment == null){
            shopFragment = new ShopFragment();
            transaction.add(R.id.menu_fragment, shopFragment);
        }
        hideFragment(transaction);
        transaction.show(shopFragment);
        transaction.commit();
    }

    private void initCenter(){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        if(centerFragment == null){
            centerFragment = new CenterFragment();
            transaction.add(R.id.menu_fragment, centerFragment);
        }
        hideFragment(transaction);
        transaction.show(centerFragment);
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction){
        if(homeFragment != null){
            transaction.hide(homeFragment);
        }
        if(shopFragment != null){
            transaction.hide(shopFragment);
        }
        if(centerFragment != null){
            transaction.hide(centerFragment);
        }
    }
}